package com.kehutong.crm.dao;

import com.kehutong.crm.entity.DemandForecast;
import org.coraframework.orm.annotation.Dao;
import org.coraframework.orm.annotation.Mapper;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.Condition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求预测dao
 *
 * @author wupengfei
 */
@Dao
public interface DemandForecastDao {

    @Mapper(
            mainDB = true,
            genericType = HashMap.class,
            sql = "SELECT sum(forecastValue) as 'forecastValue', userId, count(*) as number, type FROM t_demand_forecast where ${} group by userId, type;"
    )
    List<Map<String, Object>> statisticsByUserAndDate(Condition... condition);

    @Mapper(
            mainDB = true,
            genericType = HashMap.class,
            sql = "select corpIds, type, count(*) as number, weekStartDate from t_demand_forecast where ${} group by corpIds, type;"
    )
    List<Map<String, Object>> statisticsCompany(Condition... condition);

    @Mapper(
            mainDB = true,
            genericType = DemandForecast.class,
            countSQL = "select count(*) from (select tdfs.id, tdfs.corpId, tdf.mobile,tdf.userId,tdf.forecastValue,tdf.weekStartDate,tdf.sequence,tdf.requestId,tdf.type,tdf.createTime,tdf.createUserId,tdf.updateTime,tdf.updateUserId from t_demand_forecast tdf left join t_demand_forecast_sequence tdfs on tdf.userId = tdfs.userId and tdf.type = tdfs.type order by tdfs.sequence) a where ${}",
            sql = "select * from (select tdfs.id, tdfs.corpId, tdf.mobile,tdf.userId,tdf.forecastValue,tdf.weekStartDate,tdf.sequence,tdf.requestId,tdf.type,tdf.createTime,tdf.createUserId,tdf.updateTime,tdf.updateUserId from t_demand_forecast tdf left join t_demand_forecast_sequence tdfs on tdf.userId = tdfs.userId and tdf.type = tdfs.type order by tdfs.sequence) a where ${}"
    )
    Page<DemandForecast> getPage(Condition... condition);

}
